package com.quanyan.place.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.quanyan.place.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.enums.SysParameterConfigEnum;
import com.quanyan.common.utils.CommonUtils;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.orderpay.request.ReqOrderRefund;
import com.quanyan.orderpay.request.ReqRefundPayInfo;
import com.quanyan.orderpay.request.ReqRefundProduct;
import com.quanyan.orderpay.service.OrderPayService;
import com.quanyan.pay.enums.RefundStatusEnum;
import com.quanyan.place.biz.IBaseService;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlace;
import com.quanyan.place.entity.TbBizPlaceAttach;
import com.quanyan.place.entity.TbBizPlaceMemberCard;
import com.quanyan.place.entity.TbBizPlaceMemberCardExample;
import com.quanyan.place.entity.TbBizPlaceMemberCardType;
import com.quanyan.place.entity.TbBizPlaceMemberOpHistory;
import com.quanyan.place.entity.TbBizPlaceMemberOpHistoryExample;
import com.quanyan.place.entity.TbBizPlaceNoticeMobile;
import com.quanyan.place.entity.TbBizPlaceOrder;
import com.quanyan.place.entity.TbBizPlaceOrderDiscountItem;
import com.quanyan.place.entity.TbBizPlaceOrderDiscountItemExample;
import com.quanyan.place.entity.TbBizPlaceOrderExample;
import com.quanyan.place.entity.TbBizPlaceOrderItem;
import com.quanyan.place.entity.TbBizPlaceOrderItemExample;
import com.quanyan.place.entity.TbBizPlacePayItem;
import com.quanyan.place.entity.TbBizPlacePayItemExample;
import com.quanyan.place.entity.TbBizPlacePrivateOrder;
import com.quanyan.place.entity.TbBizPlacePrivateOrderExample;
import com.quanyan.place.entity.TbCommonParameter;
import com.quanyan.place.entity.TbPlaceTicketSalePlan;
import com.quanyan.place.entity.TbPlaceTicketSalePlanExample;
import com.quanyan.place.entity.apireq.PlaceRefundInfo;
import com.quanyan.place.entity.apireq.ReqOrderHistory;
import com.quanyan.place.entity.apireq.ReqOrderOperate;
import com.quanyan.place.entity.apiresp.RespPlaceOrderDiscountItem;
import com.quanyan.place.enums.PayTypeEnum;
import com.quanyan.place.mapper.PlaceMapper;
import com.quanyan.place.mapper.PlaceOrderAdminMapper;
import com.quanyan.place.mapper.PlaceUnitSalePlanMapper;
import com.quanyan.place.mapper.RefundOrderMapper;
import com.quanyan.place.mapper.TbBizPlaceMemberCardMapper;
import com.quanyan.place.mapper.TbBizPlaceMemberMapper;
import com.quanyan.place.mapper.TbBizPlaceMemberOpHistoryMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderDiscountItemMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderItemMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderMapper;
import com.quanyan.place.mapper.TbBizPlacePayItemMapper;
import com.quanyan.place.mapper.TbBizPlacePrivateOrderMapper;
import com.quanyan.place.mapper.TbBizPlaceUnitSalePlanMapper;
import com.quanyan.place.mapper.TbPlaceTicketSalePlanMapper;
import com.quanyan.user.response.RespUserInfoBase;
import com.quanyan.user.serviceFacade.UserServiceFacade;

import sms.SmsApi;

/**
 * Created by youxiaojia on 2016/11/23.
 */
@Service("placeOrderCallbackWrapper")
public class PlaceOrderCallbackServiceImpl implements PlaceOrderCallbackService {
    private static final Logger logger = LoggerFactory.getLogger(PlaceOrderCallbackServiceImpl.class);

    @Autowired
    private TbBizPlaceOrderMapper tbBizPlaceOrderMapper;
    @Autowired
    private TbBizPlacePayItemMapper tbBizPlacePayItemMapper;
    @Autowired
    TbBizPlaceUnitSalePlanMapper tbBizPlaceUnitSalePlanMapper;
    @Autowired
    PlaceUnitSalePlanMapper placeUnitSalePlanMapper;
    @Autowired
    TbBizPlaceOrderItemMapper tbBizPlaceOrderItemMapper;
    @Autowired
    TbBizPlacePrivateOrderMapper tbBizPlacePrivateOrderMapper;
    @Autowired
    IBaseService iBaseService;
    @Autowired
    TbBizPlaceMemberOpHistoryMapper tbBizPlaceMemberOpHistoryMapper;
    @Autowired
    PlaceMemberService placeMemberService;
    @Autowired
    PlaceTicketService placeTicketService;
    @Autowired
    PlaceGoodsService placeGoodsService;
    @Autowired
    UserServiceFacade userServiceFacade;
    @Autowired
    TbPlaceTicketSalePlanMapper tbPlaceTicketSalePlanMapper;
    @Autowired
    OrderPayService orderPayService;
    @Autowired
    PlaceMapper placeMapper;
    @Autowired
    TbBizPlaceMemberMapper tbBizPlaceMemberMapper;
    @Autowired
    TbBizPlaceMemberCardMapper tbBizPlaceMemberCardMapper;
    @Autowired
    PlaceBaseService placeBaseService;
    @Autowired
    PlaceMessageService placeMessageService;
    @Autowired
    PlaceOrderService placeOrderService;
    @Autowired
    PlaceOrderUtilService placeOrderUtilService;
    @Autowired
    com.quanyan.place.biz.SmsService  placeSmsService;
    @Autowired
    TbBizPlaceOrderDiscountItemMapper tbBizPlaceOrderDiscountItemMapper;
    @Autowired
    ApiPlaceTicketService apiPlaceTicketService;
	@Autowired
	BaseService baseService;
	
	@Autowired
	RefundOrderMapper refundOrderMapper;
	@Autowired
	PlaceOrderAdminMapper placeOrderAdminMapper;

    @Autowired
    PlaceInnerService placeInnerService;
    /*********************************************      开单 回调        *********************************************/
    /**
     * 取消订单
     * @param reqOrderOperate
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> billingOrderCancelOperate(ReqOrderOperate reqOrderOperate) {
        //取消订单  -- 订单状态==关闭
        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setOrderStatus(PlaceConstants.ORDER_CLOSE_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(date);
        record.setEndTime(date);

        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceOrder> tbBizPlaceOrders1 = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        if (tbBizPlaceOrders1.get(0).getOrderStatus() > 1){
            return APIResponse.returnFail("订单状态已改变,不能关闭！");
        }
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample1);


        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders1){
            orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }

        //回调允许更新库存
        iBaseService.placeBillingUpdatePlaceUnitSalePlan(tbBizPlaceOrderItemList,PlaceConstants.PLACE_PREORDER_STATUS);

        //释放场地状态更新场地价格
        try {
            placeInnerService.syncSalePlanPriceForRefundOrder(tbBizPlaceOrders1.get(0).getPlaceOrderId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return APIResponse.returnSuccess();
    }

    /**
     * 支付完成
     * @param reqOrderOperate
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> billingOrderCompletePayOperate(ReqOrderOperate reqOrderOperate) {

        reqOrderOperate.setUpdateStock(true);//订单完成允许回调修改库存

        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setOrderStatus(PlaceConstants.ORDER_PAYED_STATUS);
        record.setPayStatus(PlaceConstants.PAY_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(reqOrderOperate.getPayTime());
        record.setEndTime(date);

        //开单情况
        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlaceOrderExample1.setOrderByClause(" id for update ");

        if (!StringUtils.isEmpty(reqOrderOperate.getOrderSystemNo())){
            record.setOrderNo(reqOrderOperate.getOrderSystemNo());
        }
        List<TbBizPlaceOrder> tbBizPlaceOrders1 = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        if (CollectionUtils.isEmpty(tbBizPlaceOrders1)){
            return APIResponse.returnFail("回调--找不到该订单！");
        }
        if (!tbBizPlaceOrders1.get(0).getOrderStatus().equals(PlaceConstants.ORDER_PREPAY_STATUS) && !tbBizPlaceOrders1.get(0).getPayStatus().equals(PlaceConstants.NOT_PAY_STATUS)){
            return APIResponse.returnFail("订单不是未支付状态！");
        }
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample1);
        
        //更新父订单状态
        if (PlaceConstants.ORDER_USE_BLOCK_BOOKING == tbBizPlaceOrders1.get(0).getUseType()) {
        	placeOrderService.checkBlockOrder(tbBizPlaceOrders1.get(0), PlaceConstants.ORDER_PAYED_STATUS, null);
        }


        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders1){
            orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }

        
        BigDecimal receivedAmount = new BigDecimal(0);
        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemList){
            receivedAmount = receivedAmount.add(tbBizPlaceOrderItem.getSalePrice());
        }
        
        //增加pos支付明细
        TbBizPlaceOrder tOrder=tbBizPlaceOrders1.get(0);
	   	 if(!StringUtils.isEmpty(tOrder.getIsType())&&tOrder.getIsType()==1){//如果是pos，订单生成支付明细
	   		TbBizPlacePayItemExample tbBizPlacePayItemExample=new TbBizPlacePayItemExample();
	   		TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria=tbBizPlacePayItemExample.createCriteria();
	   		tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tOrder.getPlaceOrderId());
	   		List<TbBizPlacePayItem> payItems= tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
	   		if(CollectionUtils.isEmpty(payItems)){//防止重复回调生成支付明细
	   			TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
					tbBizPlacePayItem.setPlaceOrderId(tOrder.getPlaceOrderId());
					tbBizPlacePayItem.setAmount(receivedAmount);
					tbBizPlacePayItem.setAmountType(PlaceConstants.DEBIT_TYPE);
					tbBizPlacePayItem.setPayType(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY));
					tbBizPlacePayItem.setAdminPayType(PayTypeEnum.getEnum(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY)).getUnifiedId());
					if (tbBizPlacePayItemMapper.insertSelective(tbBizPlacePayItem) != 0) {
						logger.debug("========================插入订单支付数据:TbBizPlacePayItem:{}",
								JSON.toJSONString(tbBizPlacePayItem));
		             }  
	   		  }
	   	      	
	        }
        //库存改变不交给回调
        if (reqOrderOperate.isUpdateStock()){
            //回调允许更新库存
            iBaseService.placeBillingUpdatePlaceUnitSalePlan(tbBizPlaceOrderItemList,PlaceConstants.PLACE_ORDER_STATUS);
        }

        
		Integer serviceSwitch = 0;//拆分开关 1.开,0.关  和APP共用一个
	    TbCommonParameter parameter1 = baseService.getParameterByKey(SysParameterConfigEnum.PLACE_CONTAIN_VIP.getCode());
        if (parameter1 != null && parameter1.getParameterValue() != null) {
        	serviceSwitch = Integer.valueOf(parameter1.getParameterValue());
        }
        
        if (serviceSwitch == 0) {
        	  //插入支付流水
            ReqOrderHistory reqOrderHistory = placeOrderUtilService.tbBizPlaceOrder2ReqOrderHistory(tbBizPlaceOrders1.get(0),receivedAmount,(byte)0,(byte)0);
            try {
                placeOrderUtilService.insertOrderHistory(reqOrderHistory);
            } catch (Exception e) {
                e.printStackTrace();
            }
		} else {
	        //明细转流水
	        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders1){
	            List<ReqOrderHistory> reqOrderHistoryList = placeOrderUtilService.tbBizPlaceOrderItem2ReqOrderHistory(tbBizPlaceOrder,tbBizPlaceOrderItemList,(byte)0,(byte)0,null);
                try {
                    placeOrderUtilService.shoppingCartOrderInsertOrderHistory(reqOrderHistoryList);
                } catch (Exception e) {
                    e.printStackTrace();
                    return APIResponse.returnFail(e.getMessage());
                }
	        }
		}
        return APIResponse.returnSuccess("订单支付完成！");
    }

    /**
     * 退款中
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> billingOrderUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        //退款默认设置成退款中

        //修改销售计划   如需锁定库存打开以下代码
        /*TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        try {
            placeOrderServiceWrapper.refundUpdateUnitSalePlan(tbBizPlaceOrderItems,PlaceConstants.PLACE_LOCk_STATUS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return APIResponse.returnSuccess("订单设置退款中，场地状态锁定！");*/
        return APIResponse.returnSuccess("订单设置退款中！");
    }

    /**
     * 退款完成
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> billingOrderCompleteUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder){
        TbBizPlaceOrder record = new TbBizPlaceOrder();
        //设置已退款状态
        if (reqOrderOperate.getExceptionFlag()){
            record.setPayStatus(PlaceConstants.REFUNDING_EXCEPTION_STATUS);
        }
        else {
            record.setPayStatus(PlaceConstants.REFUNDING_STATUS);
        }

        BigDecimal debit = new BigDecimal(0);
        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria = tbBizPlacePayItemExample.createCriteria();
        tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlacePayItem> tbBizPlacePayItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
        if (CollectionUtils.isEmpty(tbBizPlacePayItemList)){
            return APIResponse.returnFail("场馆订单支付明细异常");
        }
        
        for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemList){
            debit = debit.add(tbBizPlacePayItem.getAmount());
        }

        //1.设置订单状态已经退款状态
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());

        record.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setRefundTime(date);
        record.setEndTime(date);
        record.setCredit(debit);
        List<TbBizPlaceOrder> tbBizPlaceOrderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample);

        //2.更改订单支付明细的状态为退款状态
        TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
        tbBizPlacePayItem.setAmountType(PlaceConstants.CREDIT_TYPE);
        tbBizPlacePayItemMapper.updateByExampleSelective(tbBizPlacePayItem, tbBizPlacePayItemExample);

        //库存改变不交给回调
        /*
        //修改销售计划
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);

        try {
            placeOrderServiceWrapper.refundUpdateUnitSalePlan(tbBizPlaceOrderItems,PlaceConstants.PLACE_PREORDER_STATUS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        */
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        //3.修改订单明细状态为已退款
        BigDecimal receivedAmount = new BigDecimal(0);
        if(CollectionUtils.isNotEmpty(tbBizPlaceOrderItems)){
	        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItems){
	            receivedAmount = receivedAmount.add(tbBizPlaceOrderItem.getSalePrice());
	            tbBizPlaceOrderItem.setStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
	            tbBizPlaceOrderItemMapper.updateByPrimaryKey(tbBizPlaceOrderItem);
	        }
        }
        
        //4.修改优惠明细状态
        Map params=new HashMap<>();
		params.put("placeOrderId", tbBizPlaceOrder.getPlaceOrderId());
        List<RespPlaceOrderDiscountItem> orderDiscountItems=placeOrderAdminMapper.getDiscountItem(params);
        if(CollectionUtils.isNotEmpty(orderDiscountItems)){
        	 for(RespPlaceOrderDiscountItem discountItem:orderDiscountItems){
     			discountItem.setStatus((int)PlaceConstants.ORDER_REFUND_STATUS);
     			placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
     		}
        }
        
        //5. 修改退款单状态
		  PlaceRefundInfo placeRefundInfo=new PlaceRefundInfo();
		  placeRefundInfo.setRefundNo(reqOrderOperate.getOutTradeRefundOrderNo());
		  placeRefundInfo.setRefundStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
		  placeRefundInfo.setRefundTime(new Date());
		  placeRefundInfo.setOrderNo(reqOrderOperate.getOrderNo());		
		  placeRefundInfo.setRefundOrderNo(reqOrderOperate.getRefundOrderNo());
		  refundOrderMapper.updateRefundOrderStatus(placeRefundInfo);
		  
        if (CollectionUtils.isNotEmpty(tbBizPlaceOrderItems) && CollectionUtils.isNotEmpty(tbBizPlaceOrderList)){
            if (tbBizPlaceOrderList.get(0).getOrderStatus()< 5 ) {
                //插入退款流水
                ReqOrderHistory reqOrderHistory = placeOrderUtilService.tbBizPlaceOrder2ReqOrderHistory(tbBizPlaceOrderList.get(0), receivedAmount, (byte) 1, (byte) 0);
                try {
                    placeOrderUtilService.insertOrderHistory(reqOrderHistory);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return APIResponse.returnSuccess("订单退款成功！");
    }

    /**
     * 退款异常
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> billingOrderCancleToUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        //退款默认设置成退款中
        return APIResponse.returnSuccess();
    }

    /*********************************************      小商品 回调        *********************************************/
    /**
     * 取消订单
     * @param reqOrderOperate
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> goodsOrderCancelOperate(ReqOrderOperate reqOrderOperate) {
        //取消订单  -- 订单状态==关闭
        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setOrderStatus(PlaceConstants.ORDER_CLOSE_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(date);
        record.setEndTime(date);

        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceOrder> tbBizPlaceOrders1 = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        if (tbBizPlaceOrders1.get(0).getOrderStatus() > 1){
            return APIResponse.returnFail("订单状态已改变,不能关闭！");
        }
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample1);


        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders1){
            orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }
        return APIResponse.returnSuccess();
    }

    /**
     * 支付完成
     * @param reqOrderOperate
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> goodsOrderCompletePayOperate(ReqOrderOperate reqOrderOperate) {

        reqOrderOperate.setUpdateStock(true);//订单完成允许回调修改库存

        //设置订单状态为已经付款状态
        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());

        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setOrderStatus(PlaceConstants.ORDER_PAYED_STATUS);
        record.setPayStatus(PlaceConstants.PAY_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(reqOrderOperate.getPayTime());
        record.setEndTime(date);
        if (!StringUtils.isEmpty(reqOrderOperate.getOrderSystemNo())){
            record.setOrderNo(reqOrderOperate.getOrderSystemNo());
        }
        List<TbBizPlaceOrder> tbBizPlaceOrders = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        if (!tbBizPlaceOrders.get(0).getOrderStatus().equals(PlaceConstants.ORDER_PREPAY_STATUS) && !tbBizPlaceOrders.get(0).getPayStatus().equals(PlaceConstants.NOT_PAY_STATUS)){
            return APIResponse.returnFail("订单不是未支付状态！");
        }
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample1);

        TbBizPlaceOrder tbBizPlaceOrder = new TbBizPlaceOrder();
        tbBizPlaceOrder.setPlaceOrderId(reqOrderOperate.getOrderNo());


        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrderHistory : tbBizPlaceOrders){
            orderStr.add(tbBizPlaceOrderHistory.getPlaceOrderId());
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }

        BigDecimal receivedAmount = new BigDecimal(0);
        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemList){
            receivedAmount = receivedAmount.add(tbBizPlaceOrderItem.getSalePrice());
        }
        
        //增加pos支付明细
        TbBizPlaceOrder tOrder=tbBizPlaceOrders.get(0);
	   	 if(!StringUtils.isEmpty(tOrder.getIsType())&&tOrder.getIsType()==1){//如果是pos，订单生成支付明细
	   		TbBizPlacePayItemExample tbBizPlacePayItemExample=new TbBizPlacePayItemExample();
	   		TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria=tbBizPlacePayItemExample.createCriteria();
	   		tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tOrder.getPlaceOrderId());
	   		List<TbBizPlacePayItem> payItems= tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
	   		if(CollectionUtils.isEmpty(payItems)){//防止重复回调生成支付明细
	   			TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
					tbBizPlacePayItem.setPlaceOrderId(tOrder.getPlaceOrderId());
					tbBizPlacePayItem.setAmount(receivedAmount);
					tbBizPlacePayItem.setAmountType(PlaceConstants.DEBIT_TYPE);
					tbBizPlacePayItem.setPayType(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY));
					tbBizPlacePayItem.setAdminPayType(PayTypeEnum.getEnum(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY)).getUnifiedId());
					if (tbBizPlacePayItemMapper.insertSelective(tbBizPlacePayItem) != 0) {
						logger.debug("========================插入订单支付数据:TbBizPlacePayItem:{}",
								JSON.toJSONString(tbBizPlacePayItem));
		             }  
	   		  }
	   		
	   	 }
	   	 
 
        //插入支付流水
        ReqOrderHistory reqOrderHistory = placeOrderUtilService.tbBizPlaceOrder2ReqOrderHistory(tbBizPlaceOrders.get(0),receivedAmount,(byte)0,(byte)0);
        try {
            placeOrderUtilService.insertOrderHistory(reqOrderHistory);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return APIResponse.returnSuccess("订单支付完成！");
    }

    /**
     * 退款中
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> goodsOrderUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        return APIResponse.returnSuccess();
    }

    /**
     * 退款完成
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> goodsOrderCompleteUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        TbBizPlaceOrder record = new TbBizPlaceOrder();
        //设置支付状态
        if (reqOrderOperate.getExceptionFlag()){
            record.setPayStatus(PlaceConstants.REFUNDING_EXCEPTION_STATUS);
        }
        else {
            record.setPayStatus(PlaceConstants.REFUNDING_STATUS);
        }

        BigDecimal debit = new BigDecimal(0);
        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria = tbBizPlacePayItemExample.createCriteria();
        tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlacePayItem> tbBizPlacePayItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
        if (CollectionUtils.isEmpty(tbBizPlacePayItemList)){
            return APIResponse.returnFail("场馆订单支付明细异常");
        }
        for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemList){
            debit = debit.add(tbBizPlacePayItem.getAmount());
        }

        //设置订单状态和支付状态为已经退款状态
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceOrder> tbBizPlaceOrderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);

        record.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setRefundTime(date);
        record.setEndTime(date);
        record.setCredit(debit);
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample);

        //更改订单支付明细的状态为退款状态
        TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
        tbBizPlacePayItem.setAmountType(PlaceConstants.CREDIT_TYPE);
        tbBizPlacePayItemMapper.updateByExampleSelective(tbBizPlacePayItem, tbBizPlacePayItemExample);

        //TODO 修改库存

        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);

        BigDecimal receivedAmount = new BigDecimal(0);
        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItems){
            receivedAmount = receivedAmount.add(tbBizPlaceOrderItem.getSalePrice());
            tbBizPlaceOrderItem.setStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
            tbBizPlaceOrderItemMapper.updateByPrimaryKey(tbBizPlaceOrderItem);
        }

        //3.修改优惠明细状态
        Map params=new HashMap<>();
		params.put("placeOrderId", tbBizPlaceOrder.getPlaceOrderId());
        List<RespPlaceOrderDiscountItem> orderDiscountItems=placeOrderAdminMapper.getDiscountItem(params);
        if(CollectionUtils.isNotEmpty(orderDiscountItems)){
        	 for(RespPlaceOrderDiscountItem discountItem:orderDiscountItems){
     			discountItem.setStatus((int)PlaceConstants.ORDER_REFUND_STATUS);
     			placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
     		}
        }
       
		
        //5. 修改退款单状态
		  PlaceRefundInfo placeRefundInfo=new PlaceRefundInfo();
		  placeRefundInfo.setRefundNo(reqOrderOperate.getOutTradeRefundOrderNo());
		  placeRefundInfo.setRefundStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
		  placeRefundInfo.setRefundTime(new Date());
		  placeRefundInfo.setOrderNo(reqOrderOperate.getOrderNo());		
		  placeRefundInfo.setRefundOrderNo(reqOrderOperate.getRefundOrderNo());
		  refundOrderMapper.updateRefundOrderStatus(placeRefundInfo);
	    

        if (CollectionUtils.isNotEmpty(tbBizPlaceOrderItems) && CollectionUtils.isNotEmpty(tbBizPlaceOrderList)){
            if (tbBizPlaceOrderList.get(0).getOrderStatus()< 5 ){
                //插入退款流水
                ReqOrderHistory reqOrderHistory = placeOrderUtilService.tbBizPlaceOrder2ReqOrderHistory(tbBizPlaceOrderList.get(0),receivedAmount,(byte)1,(byte)0);
                try {
                    placeOrderUtilService.insertOrderHistory(reqOrderHistory);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return APIResponse.returnSuccess("订单退款成功！");
    }

    /**
     * 退款异常
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> goodsOrderCancleToUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        return APIResponse.returnSuccess();
    }

    /*********************************************      包场 回调        *********************************************/
    /**
     * 取消订单
     * @param reqOrderOperate
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> bookAllOrderCancelOperate(ReqOrderOperate reqOrderOperate) {
        //取消订单  -- 订单状态==关闭
        TbBizPlacePrivateOrder record = new TbBizPlacePrivateOrder();
        record.setOrderStatus(PlaceConstants.ORDER_CLOSE_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(date);
        record.setEndTime(date);

        //修改父订单支付状态
        TbBizPlacePrivateOrderExample tbBizPlacePrivateOrderExample2 = new TbBizPlacePrivateOrderExample();
        TbBizPlacePrivateOrderExample.Criteria tbBizPlacePrivateOrderCriteria2 = tbBizPlacePrivateOrderExample2.createCriteria();
        tbBizPlacePrivateOrderCriteria2.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlacePrivateOrder> tbBizPlacePrivateOrderList = tbBizPlacePrivateOrderMapper.selectByExample(tbBizPlacePrivateOrderExample2);
        if (StringUtils.isEmpty(tbBizPlacePrivateOrderList)){
            return APIResponse.returnFail("包场订单异常");
        }
        if (tbBizPlacePrivateOrderList.get(0).getOrderStatus() > 1){
            return APIResponse.returnFail("订单状态已改变,不能关闭！");
        }
        tbBizPlacePrivateOrderMapper.updateByExampleSelective(record, tbBizPlacePrivateOrderExample2);

        //修改子订单支付状态
        TbBizPlaceOrder record1 = new TbBizPlaceOrder();
        record1.setOrderStatus(PlaceConstants.ORDER_CLOSE_STATUS);
        record1.setOrderStatusChangeDate(date);
        record1.setPaymentTime(date);
        record1.setEndTime(date);
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        //未付款的订单
        tbBizPlaceOrderExample.createCriteria().andOrderStatusLessThan(PlaceConstants.ORDER_PAYED_STATUS)
                .andParentIdEqualTo(tbBizPlacePrivateOrderList.get(0).getId());
        List<TbBizPlaceOrder> tbBizPlaceOrders = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        tbBizPlaceOrderMapper.updateByExampleSelective(record1,tbBizPlaceOrderExample);


        //回调允许更新库存
        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders){
            orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
        }
        if (CollectionUtils.isEmpty(orderStr) || orderStr.size() <=0){
            return APIResponse.returnFail("包场取消订单异常！");
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }

        //修改销售计划状态
        iBaseService.placeBillingUpdatePlaceUnitSalePlan(tbBizPlaceOrderItemList,PlaceConstants.PLACE_PREORDER_STATUS);

        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders){
            //释放场地状态更新场地价格
            try {
                placeInnerService.syncSalePlanPriceForRefundOrder(tbBizPlaceOrder.getPlaceOrderId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return APIResponse.returnSuccess();
    }

    /**
     * 支付完成
     * @param reqOrderOperate
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> bookAllOrderCompletePayOperate(ReqOrderOperate reqOrderOperate) {

        reqOrderOperate.setUpdateStock(true);//订单完成允许回调修改库存

        TbBizPlacePrivateOrder record = new TbBizPlacePrivateOrder();
        record.setOrderStatus(PlaceConstants.ORDER_PAYED_STATUS);
        record.setPayStatus(PlaceConstants.PAY_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(reqOrderOperate.getPayTime());
        record.setEndTime(date);

        if (!StringUtils.isEmpty(reqOrderOperate.getOrderSystemNo())){
            record.setOrderNo(reqOrderOperate.getOrderSystemNo());
        }

        //修改父订单支付状态
        TbBizPlacePrivateOrderExample tbBizPlacePrivateOrderExample2 = new TbBizPlacePrivateOrderExample();
        TbBizPlacePrivateOrderExample.Criteria tbBizPlacePrivateOrderCriteria2 = tbBizPlacePrivateOrderExample2.createCriteria();
        tbBizPlacePrivateOrderCriteria2.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlacePrivateOrder> tbBizPlacePrivateOrderList = tbBizPlacePrivateOrderMapper.selectByExample(tbBizPlacePrivateOrderExample2);
        if (CollectionUtils.isEmpty(tbBizPlacePrivateOrderList)){
            return APIResponse.returnFail("包场订单异常");
        }
        if (!tbBizPlacePrivateOrderList.get(0).getOrderStatus().equals(PlaceConstants.ORDER_PREPAY_STATUS) && !tbBizPlacePrivateOrderList.get(0).getPayStatus().equals(PlaceConstants.NOT_PAY_STATUS)){
            return APIResponse.returnFail("父订单不是未支付状态！");
        }
        tbBizPlacePrivateOrderMapper.updateByExampleSelective(record, tbBizPlacePrivateOrderExample2);


        //修改子订单支付状态
        TbBizPlaceOrder record1 = new TbBizPlaceOrder();
        record1.setOrderStatus(PlaceConstants.ORDER_PAYED_STATUS);
        record1.setPayStatus(PlaceConstants.PAY_STATUS);
        record1.setOrderStatusChangeDate(date);
        record1.setPaymentTime(reqOrderOperate.getPayTime());
        record1.setEndTime(date);
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        tbBizPlaceOrderExample.createCriteria().andParentIdEqualTo(tbBizPlacePrivateOrderList.get(0).getId());
        tbBizPlaceOrderMapper.updateByExampleSelective(record1,tbBizPlaceOrderExample);
        
        //库存改变不交给回调
        if (reqOrderOperate.isUpdateStock()){
            //回调允许更新库存
            List<TbBizPlaceOrder> tbBizPlaceOrders = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
            List<String> orderStr = new ArrayList<>();
            for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders){
                orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
            }
            TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
            TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
            tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
            List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
            if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
                return APIResponse.returnFail("订单明细异常！");
            }

            if (CollectionUtils.isNotEmpty(tbBizPlaceOrders)){
                for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders){
                    TbBizPlaceOrderItemExample tbBizPlaceOrderItemExampleHistory = new TbBizPlaceOrderItemExample();
                    TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteriaHistory = tbBizPlaceOrderItemExampleHistory.createCriteria();
                    tbBizPlaceOrderItemCriteriaHistory.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
                    List<TbBizPlaceOrderItem> tbBizPlaceOrderItemListHistory = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExampleHistory);
                    BigDecimal receivedAmount = new BigDecimal(0);
                    for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemListHistory){
                        receivedAmount = receivedAmount.add(tbBizPlaceOrderItem.getSalePrice());
                    }
                    
                    //增加pos支付明细
               	 if(!StringUtils.isEmpty(tbBizPlaceOrder.getIsType())&&tbBizPlaceOrder.getIsType()==1){//如果是pos，订单生成支付明细
               		TbBizPlacePayItemExample tbBizPlacePayItemExample=new TbBizPlacePayItemExample();
               		TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria=tbBizPlacePayItemExample.createCriteria();
               		tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
               		List<TbBizPlacePayItem> payItems= tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
               		if(CollectionUtils.isEmpty(payItems)){//防止重复回调生成支付明细
               			TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
           				tbBizPlacePayItem.setPlaceOrderId(tbBizPlaceOrder.getPlaceOrderId());
           				tbBizPlacePayItem.setAmount(receivedAmount);
           				tbBizPlacePayItem.setAmountType(PlaceConstants.DEBIT_TYPE);
           				tbBizPlacePayItem.setPayType(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY));
           				tbBizPlacePayItem.setAdminPayType(PayTypeEnum.getEnum(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY)).getUnifiedId());
           				if (tbBizPlacePayItemMapper.insertSelective(tbBizPlacePayItem) != 0) {
           					logger.debug("========================插入订单支付数据:TbBizPlacePayItem:{}",
           							JSON.toJSONString(tbBizPlacePayItem));
           	             }  
               		  }
               	      	
                    }
               
                    
                    //插入支付流水
                    ReqOrderHistory reqOrderHistory = placeOrderUtilService.tbBizPlaceOrder2ReqOrderHistory(tbBizPlaceOrder,receivedAmount,(byte)0,(byte)0);
                    try {
                        placeOrderUtilService.insertOrderHistory(reqOrderHistory);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            //修改销售计划状态
            iBaseService.placeBillingUpdatePlaceUnitSalePlan(tbBizPlaceOrderItemList,PlaceConstants.PLACE_ORDER_STATUS);
        }
        return APIResponse.returnSuccess("订单支付完成！");
    }

    /**
     * 退款中
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> bookAllOrderUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        //退款默认设置成退款中

        //修改销售计划  如需锁定库存打开以下代码
        /*TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        try {
            placeOrderServiceWrapper.refundUpdateUnitSalePlan(tbBizPlaceOrderItems,PlaceConstants.PLACE_LOCk_STATUS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return APIResponse.returnSuccess("订单设置退款中，场地状态锁定！");*/
        return APIResponse.returnSuccess("订单设置退款中！");
    }

    /**
     * 退款完成
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> bookAllOrderCompleteUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder){
   	 TbBizPlaceOrder record = new TbBizPlaceOrder();
        //设置支付状态
        if (reqOrderOperate.getExceptionFlag()){
            record.setPayStatus(PlaceConstants.REFUNDING_EXCEPTION_STATUS);
        }
        else {
            record.setPayStatus(PlaceConstants.REFUNDING_STATUS);
        }
        
        BigDecimal debit = new BigDecimal(0);
        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria = tbBizPlacePayItemExample.createCriteria();
        tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlacePayItem> tbBizPlacePayItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
        if (CollectionUtils.isEmpty(tbBizPlacePayItemList)){
            return APIResponse.returnFail("场馆订单支付明细异常");
        }
        
        for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemList){
            debit = debit.add(tbBizPlacePayItem.getAmount());
        }

        //1.设置订单状态为已经退款状态
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());

        record.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setRefundTime(date);
        record.setEndTime(date);
        record.setCredit(debit);
        List<TbBizPlaceOrder> tbBizPlaceOrderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample);

        //2.更改订单支付明细的状态为退款状态
   	 	TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
        tbBizPlacePayItem.setAmountType(PlaceConstants.CREDIT_TYPE);
        tbBizPlacePayItemMapper.updateByExampleSelective(tbBizPlacePayItem, tbBizPlacePayItemExample);
   
      
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        BigDecimal receivedAmount = new BigDecimal(0);
        //3.修改订单明细状态
        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItems){
            receivedAmount = receivedAmount.add(tbBizPlaceOrderItem.getSalePrice());
            tbBizPlaceOrderItem.setStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
            tbBizPlaceOrderItemMapper.updateByPrimaryKey(tbBizPlaceOrderItem);
        }
      
       
        //4.修改优惠明细状态
        Map params=new HashMap<>();
		params.put("placeOrderId", tbBizPlaceOrder.getPlaceOrderId());
        List<RespPlaceOrderDiscountItem> orderDiscountItems=placeOrderAdminMapper.getDiscountItem(params);
        if(CollectionUtils.isNotEmpty(orderDiscountItems)){
        	 for(RespPlaceOrderDiscountItem discountItem:orderDiscountItems){
     			discountItem.setStatus((int)PlaceConstants.ORDER_REFUND_STATUS);
     			placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
     		}
        }
       
		
        //5. 修改退款单状态
		  PlaceRefundInfo placeRefundInfo=new PlaceRefundInfo();
		  placeRefundInfo.setRefundNo(reqOrderOperate.getOutTradeRefundOrderNo());
		  placeRefundInfo.setRefundStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
		  placeRefundInfo.setRefundTime(new Date());
		  placeRefundInfo.setOrderNo(reqOrderOperate.getOrderNo());		
		  placeRefundInfo.setRefundOrderNo(reqOrderOperate.getRefundOrderNo());
		  refundOrderMapper.updateRefundOrderStatus(placeRefundInfo);
		 if (CollectionUtils.isNotEmpty(tbBizPlaceOrderItems) && CollectionUtils.isNotEmpty(tbBizPlaceOrderList)){
            if (tbBizPlaceOrderList.get(0).getOrderStatus()< 5 ) {
                //插入退款流水
                ReqOrderHistory reqOrderHistory = placeOrderUtilService.tbBizPlaceOrder2ReqOrderHistory(tbBizPlaceOrderList.get(0), receivedAmount, (byte) 1, (byte) 0);
                try {
                    placeOrderUtilService.insertOrderHistory(reqOrderHistory);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
   
       
        
        return APIResponse.returnSuccess("订单退款成功！");
    }

    /**
     * 退款异常
     * @param reqOrderOperate
     * @param tbBizPlaceOrder
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> bookAllOrderCancleToUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        //退款默认设置成退款中
        return APIResponse.returnSuccess();
    }

    /*********************************************      门票 回调        *********************************************/
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> ticketOrderCancelOperate(ReqOrderOperate reqOrderOperate) {
        //取消订单  -- 订单状态==关闭
        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setOrderStatus(PlaceConstants.ORDER_CLOSE_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(date);
        record.setEndTime(date);

        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceOrder> tbBizPlaceOrders1 = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        if (tbBizPlaceOrders1.get(0).getOrderStatus() > 1){
            return APIResponse.returnFail("订单状态已改变,不能关闭！");
        }
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample1);



        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders1){
            orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }

        return APIResponse.returnSuccess();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> ticketOrderCompletePayOperate(ReqOrderOperate reqOrderOperate) {

        reqOrderOperate.setUpdateStock(true);//订单完成允许回调修改库存

        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setOrderStatus(PlaceConstants.ORDER_PAYED_STATUS);
        record.setPayStatus(PlaceConstants.PAY_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(reqOrderOperate.getPayTime());
        record.setEndTime(date);

        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());

        if (!StringUtils.isEmpty(reqOrderOperate.getOrderSystemNo())){
            record.setOrderNo(reqOrderOperate.getOrderSystemNo());
        }
        List<TbBizPlaceOrder> tbBizPlaceOrders1 = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        if (CollectionUtils.isEmpty(tbBizPlaceOrders1)){
            return APIResponse.returnFail("门票订单异常");
        }
        if (!tbBizPlaceOrders1.get(0).getOrderStatus().equals(PlaceConstants.ORDER_PREPAY_STATUS) && !tbBizPlaceOrders1.get(0).getPayStatus().equals(PlaceConstants.NOT_PAY_STATUS)){
            return APIResponse.returnFail("订单不是未支付状态！");
        }
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample1);


        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders1){
            orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }

        //库存改变不交给回调
        if (reqOrderOperate.isUpdateStock()){
            //回调允许更新库存
            placeTicketService.ticketStatisticsForScanPay(tbBizPlaceOrders1.get(0).getPlaceId(),tbBizPlaceOrderItemList);
        }

        BigDecimal receivedAmount = new BigDecimal(0);
        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemList){
            receivedAmount = receivedAmount.add(tbBizPlaceOrderItem.getSalePrice());
        }
        
	 	   //增加pos支付明细
         TbBizPlaceOrder tOrder=tbBizPlaceOrders1.get(0);
	   	 if(!StringUtils.isEmpty(tOrder.getIsType())&&tOrder.getIsType()==1){//如果是pos，订单生成支付明细
	   		TbBizPlacePayItemExample tbBizPlacePayItemExample=new TbBizPlacePayItemExample();
	   		TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria=tbBizPlacePayItemExample.createCriteria();
	   		tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tOrder.getPlaceOrderId());
	   		List<TbBizPlacePayItem> payItems= tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
	   		if(CollectionUtils.isEmpty(payItems)){//防止重复回调生成支付明细
	   			TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
					tbBizPlacePayItem.setPlaceOrderId(tOrder.getPlaceOrderId());
					tbBizPlacePayItem.setAmount(receivedAmount);
					tbBizPlacePayItem.setAmountType(PlaceConstants.DEBIT_TYPE);
					tbBizPlacePayItem.setPayType(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY));
					tbBizPlacePayItem.setAdminPayType(PayTypeEnum.getEnum(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY)).getUnifiedId());
					if (tbBizPlacePayItemMapper.insertSelective(tbBizPlacePayItem) != 0) {
						logger.debug("========================插入订单支付数据:TbBizPlacePayItem:{}",
								JSON.toJSONString(tbBizPlacePayItem));
		             }  
	   		  }
	   	      	
	        }
 	
        
        //插入支付流水
        ReqOrderHistory reqOrderHistory = placeOrderUtilService.tbBizPlaceOrder2ReqOrderHistory(tbBizPlaceOrders1.get(0),receivedAmount,(byte)0,(byte)0);
        try {
            placeOrderUtilService.insertOrderHistory(reqOrderHistory);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return APIResponse.returnSuccess("订单支付完成！");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> ticketOrderUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        APIResponse<RespUserInfoBase> userMsg = null;
        try {
            userMsg = userServiceFacade.queryUserInfo(reqOrderOperate.getUid());
            if(!userMsg.isRet()){
                return APIResponse.returnFail("调用用户系统异常");

            }
        } catch (Exception e) {
            e.printStackTrace();
            return APIResponse.returnFail("调用用户系统异常");
        }

        RespUserInfoBase respUserInfoBase = userMsg.getData();
        reqOrderOperate.setName(respUserInfoBase.getNickName());

        //TODO 通知订单系统，退款处理，传入旧的的订单号，获取订单系统退款订单号
        List<Integer> values = new ArrayList<>();
        values.add(PlaceConstants.PAY_WAY_ALIPAY);
        values.add(PlaceConstants.PAY_WAY_WEICHAT);
        values.add(PlaceConstants.PAY_WAY_WEICHATH5);
        values.add(PlaceConstants.PAY_WAY_UNION_PAY);
        values.add(PlaceConstants.PAY_WAY_APPLE_PAY);
        values.add(PlaceConstants.PAY_WAY_FOR_CARD);

        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria = tbBizPlacePayItemExample.createCriteria();
        tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlacePayItem> tbBizPlacePayItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
        if(CollectionUtils.isEmpty(tbBizPlacePayItemList)){
            return APIResponse.returnFail("不存在订单信息！");
        }


        for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemList) {
            //判断是否为卡支付
            if(PlaceConstants.PAY_WAY_FOR_CARD.equals(tbBizPlacePayItem.getPayType())
                    || PlaceConstants.PLACE_ADMIN_PAY_WAYS_BOSS.equals(tbBizPlacePayItem.getPayType().toString())){
                return orderCardPayUnsubscribeOperate(reqOrderOperate);
            }
            //判断是否只使用优惠券支付
            if(PlaceConstants.PAY_WAY_FOR_COUPON.equals(tbBizPlacePayItem.getPayType()) && tbBizPlacePayItemList.size() == 1 ){
                return APIResponse.returnFail("只使用优惠券支付，不允许退款。");
            }
        }

        APIRequest<ReqOrderRefund> orderRefundAPIRequest = new APIRequest<>();
        ReqOrderRefund reqOrderRefund = new ReqOrderRefund();
        reqOrderRefund.setSourceBusinessId(Constants.PLACE_TICKET_CODE);
        reqOrderRefund.setSourceSystemId(Constants.PLACE_SYSTEM_CODE);
        reqOrderRefund.setOrderNo(tbBizPlaceOrder.getOrderNo());
        reqOrderRefund.setReason(reqOrderOperate.getReason());
        reqOrderRefund.setUid(reqOrderOperate.getUid());
        //reqOrderRefund.setRefundNotifyURL(refundUrl);
        reqOrderRefund.setRefundNotifyURL("");
        reqOrderRefund.setNeedAudit(false); //false不需要人工审核.  true: 需要人工审核, 根据业务需求设置
        reqOrderRefund.setOutTradeNo(reqOrderOperate.getOrderNo());
        reqOrderRefund.setOutTradeRefundNo(reqOrderOperate.getOrderNo());//业务系统退款流水号，目前为订单号
        reqOrderRefund.setVer(reqOrderOperate.getVer());
        //查询销售计划表
        List<ReqRefundProduct> reqRefundProducts = new ArrayList<>();

        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        List<String> saleId = new ArrayList<>();
        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItems){
            saleId.add(tbBizPlaceOrderItem.getSaleId());
        }
        TbPlaceTicketSalePlanExample tbPlaceTicketSalePlanExample = new TbPlaceTicketSalePlanExample();
        tbBizPlaceOrderItemExample.createCriteria().andSaleIdIn(saleId);
        List<TbPlaceTicketSalePlan> tbPlaceTicketSalePlanList = tbPlaceTicketSalePlanMapper.selectByExample(tbPlaceTicketSalePlanExample);
        for (TbPlaceTicketSalePlan placeOrderSalePlan : tbPlaceTicketSalePlanList) {
            ReqRefundProduct refundProduct = new ReqRefundProduct();
            refundProduct.setPrice(placeOrderSalePlan.getPrice());
            refundProduct.setProductId(placeOrderSalePlan.getSaleNo());
            refundProduct.setQuantity(1);
            refundProduct.setProductName(placeOrderSalePlan.getStartTime() + "-" + placeOrderSalePlan.getEndTime()+"时段");
            reqRefundProducts.add(refundProduct);
        }

        reqOrderRefund.setRefundProductList(reqRefundProducts);


        List<ReqRefundPayInfo> reqRefundPayInfoList = new ArrayList<ReqRefundPayInfo>();
        for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemList) {
            ReqRefundPayInfo  reqRefundPayInfo = new ReqRefundPayInfo();
            reqRefundPayInfo.setPayId(tbBizPlacePayItem.getPayType());
            reqRefundPayInfo.setRefundAmount(tbBizPlacePayItem.getAmount());
            if(values.contains(reqRefundPayInfo.getPayId())){
                reqOrderRefund.setAmount(tbBizPlacePayItem.getAmount());//TODO 累加
            }

            reqRefundPayInfoList.add(reqRefundPayInfo);

        }
        reqOrderRefund.setReqRefundPayInfos(reqRefundPayInfoList);


        orderRefundAPIRequest.setData(reqOrderRefund);
        APIResponse<RefundStatusEnum> apiResponse = null;
        try {
            apiResponse = this.orderPayService.refundApply(orderRefundAPIRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null == apiResponse){
            return APIResponse.returnFail("订单退款受理失败，请重试！");
        } else if (!apiResponse.isRet()){
            logger.warn("订单退款受理失败，orderNo：{}，errMsg：{}",reqOrderOperate.getOrderNo(),apiResponse.getErrmsg());
            return apiResponse;
        }

        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());

        TbBizPlaceOrder tbBizPlaceRecord = new TbBizPlaceOrder();
        tbBizPlaceRecord.setOrderStatus(PlaceConstants.ORDER_REFUNDING_STATUS);
        tbBizPlaceRecord.setOrderStatusChangeDate(new Date());
        List<TbBizPlaceOrder> tbBizPlaceOrders = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        tbBizPlaceOrderMapper.updateByExampleSelective(tbBizPlaceRecord, tbBizPlaceOrderExample1);

        if (CollectionUtils.isEmpty(tbBizPlaceOrders)){
            return APIResponse.returnFail("没有该门票订单！");
        }
        //释放库存
        try {
            placeTicketService.updateTicketStatistics(tbBizPlaceOrders.get(0),tbBizPlaceOrderItems);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*//场馆报名人数减一
        Map<String,Object> map = new HashMap<>();
        map.put("placeId", tbBizPlaceOrders.get(0).getPlaceId());
        map.put("subtractSignupCount", 1);
        placeMapper.updateCount(map);*/

        return APIResponse.returnSuccess("订单退款受理成功！");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> ticketOrderCompleteUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        TbBizPlaceOrder record = new TbBizPlaceOrder();
        //设置支付状态
        if (reqOrderOperate.getExceptionFlag()){
            record.setPayStatus(PlaceConstants.REFUNDING_EXCEPTION_STATUS);
        }
        else {
            record.setPayStatus(PlaceConstants.REFUNDING_STATUS);
        }

        BigDecimal debit = new BigDecimal(0);
        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria = tbBizPlacePayItemExample.createCriteria();
        tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlacePayItem> tbBizPlacePayItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
        if (CollectionUtils.isEmpty(tbBizPlacePayItemList)){
            return APIResponse.returnFail("场馆订单支付明细异常");
        }
        for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemList){
            debit = debit.add(tbBizPlacePayItem.getAmount());
        }

        //设置订单状态和支付状态为已经退款状态
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());

        record.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setRefundTime(date);
        record.setEndTime(date);
        record.setCredit(debit);
        List<TbBizPlaceOrder> tbBizPlaceOrderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample);

        //更改订单支付明细的状态为退款状态
        TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
        tbBizPlacePayItem.setAmountType(PlaceConstants.CREDIT_TYPE);
        tbBizPlacePayItemMapper.updateByExampleSelective(tbBizPlacePayItem, tbBizPlacePayItemExample);

        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);

        BigDecimal receivedAmount = new BigDecimal(0);
        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItems){
            receivedAmount = receivedAmount.add(tbBizPlaceOrderItem.getSalePrice());
            tbBizPlaceOrderItem.setStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
            tbBizPlaceOrderItemMapper.updateByPrimaryKey(tbBizPlaceOrderItem);
        }

        //3.修改优惠明细状态
        Map params=new HashMap<>();
		params.put("placeOrderId", tbBizPlaceOrder.getPlaceOrderId());
        List<RespPlaceOrderDiscountItem> orderDiscountItems=placeOrderAdminMapper.getDiscountItem(params);
        if(CollectionUtils.isNotEmpty(orderDiscountItems)){
        	 for(RespPlaceOrderDiscountItem discountItem:orderDiscountItems){
     			discountItem.setStatus((int)PlaceConstants.ORDER_REFUND_STATUS);
     			placeOrderAdminMapper.updateDiscountItemByPK(discountItem);
     		}
        }
       
		
        //5. 修改退款单状态
		  PlaceRefundInfo placeRefundInfo=new PlaceRefundInfo();
		  placeRefundInfo.setRefundNo(reqOrderOperate.getOutTradeRefundOrderNo());
		  placeRefundInfo.setRefundStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
		  placeRefundInfo.setRefundTime(new Date());
		  placeRefundInfo.setOrderNo(reqOrderOperate.getOrderNo());		
		  placeRefundInfo.setRefundOrderNo(reqOrderOperate.getRefundOrderNo());
		  refundOrderMapper.updateRefundOrderStatus(placeRefundInfo);
	    
        if (CollectionUtils.isNotEmpty(tbBizPlaceOrderItems) && CollectionUtils.isNotEmpty(tbBizPlaceOrderList)) {
            if (tbBizPlaceOrderList.get(0).getOrderStatus()< 5 ) {
                int refundNum = getNumMemberCardOrderOpNum(tbBizPlaceOrder.getPlaceOrderId());
                if (refundNum > 0) {
                    //插入退款流水
                    ReqOrderHistory reqOrderHistory = placeOrderUtilService.memberTicket2ReqOrderHistory(tbBizPlaceOrder, refundNum, (byte) 1, (byte) 0);
                    try {
                        placeOrderUtilService.insertOrderHistory(reqOrderHistory);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    apiPlaceTicketService.orderCallBackHistory(reqOrderOperate);
                }
            }
        }
        return APIResponse.returnSuccess("订单退款成功！");
    }

    @Override
    public APIResponse<?> ticketOrderCancleToUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {

        return APIResponse.returnSuccess("订单退款异常，当前订单状态退款中！");
    }

    /*********************************************      会员充值 回调        *********************************************/
    /**
     * 支付完成
     * @param reqOrderOperate
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> cardOrderCompletePayOperate(ReqOrderOperate reqOrderOperate) {
        TbBizPlaceMemberOpHistoryExample tbBizPlaceMemberOpHistoryExample = new TbBizPlaceMemberOpHistoryExample();
        tbBizPlaceMemberOpHistoryExample.createCriteria().andOrderNoEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlaceMemberOpHistoryExample.setOrderByClause(" id for update ");//加锁
        List<TbBizPlaceMemberOpHistory> tbBizPlaceMemberOpHistoryList  = tbBizPlaceMemberOpHistoryMapper
                .selectByExample(tbBizPlaceMemberOpHistoryExample);
        if (CollectionUtils.isEmpty(tbBizPlaceMemberOpHistoryList)){
            return APIResponse.returnFail("没有会员充值记录");
        }
        TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory = tbBizPlaceMemberOpHistoryList.get(0);
        tbBizPlaceMemberOpHistory.setPaymentTime(reqOrderOperate.getPayTime());
        if (tbBizPlaceMemberOpHistory.getState().byteValue() != PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_NO){
            return APIResponse.returnFail("开卡充值记录不是未支付状态！");
        }
        if (tbBizPlaceMemberOpHistory.getSrc() == PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_OPEN
                && tbBizPlaceMemberOpHistory.getState().byteValue() == PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_NO) {
            //开卡

            /*//更新会员状态
            Integer uid = tbBizPlaceMemberOpHistoryList.get(0).getUid();
            TbBizPlaceMemberExample tbBizPlaceMemberExample = new TbBizPlaceMemberExample();
            tbBizPlaceMemberExample.createCriteria().andUidEqualTo(uid);
            List<TbBizPlaceMember> tbBizPlaceMemberList = tbBizPlaceMemberMapper.selectByExample(tbBizPlaceMemberExample);
            if (CollectionUtils.isEmpty(tbBizPlaceMemberList)){
                return APIResponse.returnFail("开卡——没有该会员");
            }
            tbBizPlaceMemberList.get(0).setStatus(PlaceConstants.PLACE_MEMBER_STATUS_NORMAL);
            tbBizPlaceMemberMapper.updateByPrimaryKey(tbBizPlaceMemberList.get(0));*/

            //更新会员卡状态
            String cardNo = tbBizPlaceMemberOpHistory.getInnerCardNo();
            TbBizPlaceMemberCardExample tbBizPlaceMemberCardExample = new TbBizPlaceMemberCardExample();
            tbBizPlaceMemberCardExample.createCriteria().andInnerCardNoEqualTo(cardNo);
            tbBizPlaceMemberCardExample.setOrderByClause(" id for update ");//加锁
            List<TbBizPlaceMemberCard> tbBizPlaceMemberCardList = tbBizPlaceMemberCardMapper.selectByExample(tbBizPlaceMemberCardExample);
            if (CollectionUtils.isEmpty(tbBizPlaceMemberCardList)){
                return APIResponse.returnFail("开卡——没有该会员卡");
            }
            tbBizPlaceMemberCardList.get(0).setStatus(PlaceConstants.PLACE_MEMBER_CARD_STATUS_NORMAL);
            tbBizPlaceMemberCardList.get(0).setIsDelete(Constants.IS_DELETE_FALSE);
            tbBizPlaceMemberCardMapper.updateByPrimaryKey(tbBizPlaceMemberCardList.get(0));

            //更新流水状态
            tbBizPlaceMemberOpHistory.setState(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_YES);
            if (!StringUtils.isEmpty(reqOrderOperate.getOrderSystemNo())){
                tbBizPlaceMemberOpHistory.setNo(reqOrderOperate.getOrderSystemNo());
            }
            tbBizPlaceMemberOpHistoryMapper.updateByPrimaryKeySelective(tbBizPlaceMemberOpHistoryList.get(0));
            
            //增加会员开卡成功，发送短信提醒
     	   //根据系统参数获取客服密码
            int placeId=tbBizPlaceMemberCardList.get(0).getPlaceId();
	         TbBizPlace place=placeBaseService.getPlaceById(placeId);
	         List<String> mobileList=new ArrayList<>();
	         List<String> paramList=new ArrayList<>();
	
	         //新增开卡发送短信
	         String [] args=new String[4];
	         args[0]=DateUtils.getCurrentTime();
	         args[1]=place!=null?place.getName():"";
	         args[2]= tbBizPlaceMemberCardList.get(0).getCardName();
	         // 联系电话优先取订场咨询电话，订场电话为空时，取客服电话
	         TbBizPlaceAttach placeAttach = placeBaseService.getBizPlaceAttach(placeId);
	         if (null != placeAttach && org.apache.commons.lang3.StringUtils.isNotEmpty(placeAttach.getBookingConsultPhone())){
	             args[3]=placeAttach.getBookingConsultPhone();
	         } else {
	             // 获取系统配置的百灵鸟客服热线
	             TbCommonParameter parameterVo = baseService.getParameterByKey(SysParameterConfigEnum.CONFIG_APP_SERVICE_MOBILE.getCode());
	             args[3]=parameterVo!=null?parameterVo.getParameterValue():"";
	         }
	         mobileList.add(tbBizPlaceMemberOpHistory.getMobile());
	         paramList.addAll(Arrays.asList(args));
	         try{
	             //placeTicketService.asyncSendSms(reqMemberCard.getPhone(), SmsApi.MESSAGE_TEMPLATE_PLACE_ADD_MEMBER_INFO,args,true);
	             placeSmsService.sendSms(mobileList,SmsApi.MESSAGE_TEMPLATE_PLACE_ADD_MEMBER_INFO, PlaceConstants.PLACE_SMS_TYPE_MESSAGE,paramList);
	         }catch(Exception e){
	             logger.info("短信发送失败-------->"+e.getMessage());
	         }
	     
            

        }else if (tbBizPlaceMemberOpHistory.getSrc() == PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_RECHARGE
                && tbBizPlaceMemberOpHistory.getState().byteValue() == PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_NO){
            //充值
            //更新卡余额
            placeMemberService.updateMemberCardByHistory(tbBizPlaceMemberOpHistoryList.get(0).getOrderNo());

            //更新流水状态
            tbBizPlaceMemberOpHistory.setState(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_YES);
            if (!StringUtils.isEmpty(reqOrderOperate.getOrderSystemNo())){
                tbBizPlaceMemberOpHistory.setNo(reqOrderOperate.getOrderSystemNo());
            }
            tbBizPlaceMemberOpHistoryMapper.updateByPrimaryKeySelective(tbBizPlaceMemberOpHistory);
        }


        ReqOrderHistory reqOrderHistory = placeMemberService.tbBizPlaceMemberOpHistory2ReqOrderHistory(tbBizPlaceMemberOpHistory);
        if (null != reqOrderHistory){
            try {
                placeOrderUtilService.insertOrderHistory(reqOrderHistory);
            } catch (Exception e) {
                logger.error("会员开卡充值历史记录插入失败");
//                    throw new BizException("会员开卡充值历史记录插入失败");
            }
        }


        return APIResponse.returnSuccess("订单支付完成！");
    }

    public void placeAddMemberRemind(TbBizPlace place,TbBizPlaceNoticeMobile messageVo, TbBizPlaceMemberCardType cardType,TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory){
        //查询会员卡信息
            String[] messageArgs = new String[5];
            List<String> mobileList=new ArrayList<>();
            List<String> paramList=new ArrayList<>();
        try {
            TbBizPlaceMemberCard card = placeMemberService.getTbBizPlaceMemberCardByInnerCardNo(tbBizPlaceMemberOpHistory.getInnerCardNo());
            messageArgs[0] = tbBizPlaceMemberOpHistory != null ? tbBizPlaceMemberOpHistory.getMobile() : "";
            messageArgs[1] = DateUtils.transferDateToStringZHCN(tbBizPlaceMemberOpHistory.getCreateTime());
            messageArgs[2] = place != null ? place.getName() : "";
            if (cardType != null && cardType.getType().equals(PlaceConstants.PLACE_MEMBER_CARD_TYPE_NUMBER)) {
                messageArgs[3] = cardType.getFrequency() + "次" + cardType.getName();
                messageArgs[4] = "次数" + cardType.getFrequency()+"次";
            } else {
                messageArgs[3] = cardType.getInitAmount() + "元" + cardType.getName();
                messageArgs[4] = "开卡金额" + cardType.getInitAmount()+"元";
            }
        }catch (java.lang.Exception e) {
            logger.info(e.getMessage());
        }
        mobileList.add(messageVo.getMobile());
        paramList.addAll(Arrays.asList(messageArgs));
        try {
           //placeTicketService.asyncSendSms(messageVo.getMobile(), SmsApi.MESSAGE_TEMPLATE_PLACE_ADD_MEMBER_REMIND, messageArgs, true);
            placeSmsService.sendSms(mobileList,SmsApi.MESSAGE_TEMPLATE_PLACE_ADD_MEMBER_REMIND, PlaceConstants.PLACE_SMS_TYPE_MESSAGE,paramList);
        } catch (java.lang.Exception e) {
            logger.info("短信发送失败-------->" + e.getMessage());
        }
    }

    public void placeMemberRecharge(TbBizPlace place,TbBizPlaceNoticeMobile messageVo, TbBizPlaceMemberCardType cardType,TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory){
        String[] messageArgs = new String[5];
        List<String> mobileList=new ArrayList<>();
        List<String> paramList=new ArrayList<>();
        try {
            messageArgs[0] = tbBizPlaceMemberOpHistory != null ? tbBizPlaceMemberOpHistory.getMobile() : "";
            messageArgs[1]=DateUtils.transferDateToStringZHCN(tbBizPlaceMemberOpHistory.getCreateTime());
            messageArgs[2]=place!=null?place.getName():"";
            if (cardType!=null&&cardType.getType().equals(PlaceConstants.PLACE_MEMBER_CARD_TYPE_NUMBER)){
                messageArgs[3]=tbBizPlaceMemberOpHistory.getOpCardCount()+"次"+cardType.getName();
                messageArgs[4]="充值次数"+tbBizPlaceMemberOpHistory.getOpCardCount()+"次";
            }else{
                messageArgs[3]=tbBizPlaceMemberOpHistory.getAmount()+"元"+cardType.getName();
                messageArgs[4]="充值金额"+tbBizPlaceMemberOpHistory.getAmount()+"元";
            }
        }catch (java.lang.Exception e) {
            logger.info(e.getMessage());
        }
        mobileList.add(messageVo.getMobile());
        paramList.addAll(Arrays.asList(messageArgs));
        try {
           // placeTicketService.asyncSendSms(messageVo.getMobile(), SmsApi.MESSAGE_TEMPLATE_PLACE_MEMBER_RECHARGE, messageArgs, true);
            placeSmsService.sendSms(mobileList,SmsApi.MESSAGE_TEMPLATE_PLACE_MEMBER_RECHARGE, PlaceConstants.PLACE_SMS_TYPE_MESSAGE,paramList);
        } catch (java.lang.Exception e) {
            logger.info("短信发送失败-------->" + e.getMessage());
        }
    }

        @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> appCardChargeOrderCompletePayOperate(ReqOrderOperate reqOrderOperate,
                                                               TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory) {
            TbBizPlace place=placeBaseService.getPlaceById(tbBizPlaceMemberOpHistory.getPlaceId());
            //查询场馆人员信息
            TbBizPlaceNoticeMobile messageVo = placeMessageService.getMobileDataByPlaceId(tbBizPlaceMemberOpHistory.getPlaceId());
            //查询会员卡类型
            TbBizPlaceMemberCardType cardType=placeMemberService.getMemberTypeById(tbBizPlaceMemberOpHistory.getCardTypeId());
        if (tbBizPlaceMemberOpHistory.getSrc() == PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_OPEN
                && tbBizPlaceMemberOpHistory.getState().byteValue() == PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_NO){
            APIResponse<?> response = placeMemberService.addMemberCardByHistory(tbBizPlaceMemberOpHistory.getOrderNo(),tbBizPlaceMemberOpHistory.getRefereesId());
            if (response.isRet()){
                TbBizPlaceMemberCard card = (TbBizPlaceMemberCard) response.getData();
                tbBizPlaceMemberOpHistory.setInnerCardNo(card.getInnerCardNo());
                //APP会员开卡发送短信提醒给场馆人员
                if (null != messageVo){
                    this.placeAddMemberRemind(place,messageVo,cardType,tbBizPlaceMemberOpHistory);
                }
            }
        }else if (tbBizPlaceMemberOpHistory.getSrc() == PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_RECHARGE
                && tbBizPlaceMemberOpHistory.getState().byteValue() == PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_NO){
            placeMemberService.updateMemberCardByHistory(tbBizPlaceMemberOpHistory.getOrderNo());
            //APP会员充值发送短信提醒给场馆人员
            if (null != messageVo){
                this.placeMemberRecharge(place,messageVo,cardType,tbBizPlaceMemberOpHistory);
            }
        }
        tbBizPlaceMemberOpHistory.setState(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_YES);
        tbBizPlaceMemberOpHistory.setPaymentTime(reqOrderOperate.getPayTime());
        tbBizPlaceMemberOpHistoryMapper.updateByPrimaryKeySelective(tbBizPlaceMemberOpHistory);

            ReqOrderHistory reqOrderHistory = placeMemberService.tbBizPlaceMemberOpHistory2ReqOrderHistory(tbBizPlaceMemberOpHistory);
            if (null != reqOrderHistory){
                try {
                    placeOrderUtilService.insertOrderHistory(reqOrderHistory);
                } catch (Exception e) {
                    logger.error("会员开卡充值历史记录插入失败");
//                    throw new BizException("会员开卡充值历史记录插入失败");
                }
            }

        return APIResponse.returnSuccess("订单支付完成！");
    }

    //购物车取消订单
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> shoppingCartOrderCancelOperate(ReqOrderOperate reqOrderOperate) {
        //取消订单  -- 订单状态==关闭
        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setOrderStatus(PlaceConstants.ORDER_CLOSE_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);

        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlaceOrderCriteria1.andOrderStatusEqualTo(PlaceConstants.ORDER_PREPAY_STATUS);//待付款才可以关闭
        List<TbBizPlaceOrder> tbBizPlaceOrders1 = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        if (CollectionUtils.isEmpty(tbBizPlaceOrders1)){
            return APIResponse.returnFail("回调--找不到该订单！");
        }
        if (!tbBizPlaceOrders1.get(0).getOrderStatus().equals(PlaceConstants.ORDER_CLOSE_STATUS) && !tbBizPlaceOrders1.get(0).getPayStatus().equals(PlaceConstants.NOT_PAY_STATUS)){
            return APIResponse.returnFail("订单状态已改变,不能关闭！");
        }

        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders1){
            orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }

        //退订单状态
        int flag = tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample1);
        if (flag != 1){
            return APIResponse.returnFail("关闭订单失败");
        }
        //退订单明细
        TbBizPlaceOrderItem tbBizPlaceOrderItem = new TbBizPlaceOrderItem();
        tbBizPlaceOrderItem.setStatus(PlaceConstants.ORDER_CLOSE_STATUS.intValue());
        tbBizPlaceOrderItemMapper.updateByExampleSelective(tbBizPlaceOrderItem,tbBizPlaceOrderItemExample);

        //退优惠明细
        TbBizPlaceOrderDiscountItem tbBizPlaceOrderDiscountItem = new TbBizPlaceOrderDiscountItem();
        tbBizPlaceOrderDiscountItem.setStatus(PlaceConstants.ORDER_CLOSE_STATUS.intValue());
        TbBizPlaceOrderDiscountItemExample tbBizPlaceOrderDiscountItemExample = new TbBizPlaceOrderDiscountItemExample();
        tbBizPlaceOrderDiscountItemExample.createCriteria().andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlaceOrderDiscountItemMapper.updateByExampleSelective(tbBizPlaceOrderDiscountItem,tbBizPlaceOrderDiscountItemExample);


        //场地订单明细
        List<TbBizPlaceOrderItem> placeUnitOrderItemList = new ArrayList<>();;
        for (TbBizPlaceOrderItem tbOrderItem : tbBizPlaceOrderItemList){
            //场地
            if (tbOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
                    || tbOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)
                    || tbOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_APP_BILLING)){
                placeUnitOrderItemList.add(tbOrderItem);
            }
        }

        if (!org.springframework.util.CollectionUtils.isEmpty(placeUnitOrderItemList)){
            //释放场地库存
            try {
                placeOrderService.refundUpdateUnitSalePlan(placeUnitOrderItemList,PlaceConstants.PLACE_PREORDER_STATUS);
            } catch (Exception e) {
                e.printStackTrace();
                return APIResponse.returnFail("释放场地库存异常");
            }
            //释放场地状态更新场地价格
            try {
                placeInnerService.syncSalePlanPriceForRefundOrder(tbBizPlaceOrders1.get(0).getPlaceOrderId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return APIResponse.returnSuccess();
    }

    //购物车支付成功
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> shoppingCartOrderCompletePayOperate(ReqOrderOperate reqOrderOperate) {
        reqOrderOperate.setUpdateStock(true);//订单完成允许回调修改库存

        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setOrderStatus(PlaceConstants.ORDER_PAYED_STATUS);
        record.setPayStatus(PlaceConstants.PAY_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setPaymentTime(reqOrderOperate.getPayTime());

        TbBizPlaceOrderExample tbBizPlaceOrderExample1 = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria1 = tbBizPlaceOrderExample1.createCriteria();
        tbBizPlaceOrderCriteria1.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlaceOrderCriteria1.andOrderStatusEqualTo(PlaceConstants.ORDER_PREPAY_STATUS);//待付款才可以完成支付
        tbBizPlaceOrderExample1.setOrderByClause(" id for update ");

        if (!StringUtils.isEmpty(reqOrderOperate.getOrderSystemNo())){
            record.setOrderNo(reqOrderOperate.getOrderSystemNo());
        }
        List<TbBizPlaceOrder> tbBizPlaceOrders1 = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample1);
        if (CollectionUtils.isEmpty(tbBizPlaceOrders1)){
            return APIResponse.returnFail("回调--找不到该订单！");
        }
        if (!tbBizPlaceOrders1.get(0).getOrderStatus().equals(PlaceConstants.ORDER_PREPAY_STATUS) && !tbBizPlaceOrders1.get(0).getPayStatus().equals(PlaceConstants.NOT_PAY_STATUS)){
            return APIResponse.returnFail("订单不是未支付状态！");
        }

        List<String> orderStr = new ArrayList<>();
        for (TbBizPlaceOrder tbBizPlaceOrder : tbBizPlaceOrders1){
            orderStr.add(tbBizPlaceOrder.getPlaceOrderId());
        }
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdIn(orderStr);
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return APIResponse.returnFail("订单明细异常！");
        }

        //订单状态
        int flag = tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample1);
        if (flag != 1){
            return APIResponse.returnFail("支付订单失败");
        }
        //订单明细状态
        TbBizPlaceOrderItem tbBizPlaceOrderItem = new TbBizPlaceOrderItem();
        tbBizPlaceOrderItem.setStatus(PlaceConstants.ORDER_PAYED_STATUS.intValue());
        tbBizPlaceOrderItemMapper.updateByExampleSelective(tbBizPlaceOrderItem,tbBizPlaceOrderItemExample);

        //优惠明细
        TbBizPlaceOrderDiscountItem tbBizPlaceOrderDiscountItem = new TbBizPlaceOrderDiscountItem();
        tbBizPlaceOrderDiscountItem.setStatus(PlaceConstants.ORDER_PAYED_STATUS.intValue());
        TbBizPlaceOrderDiscountItemExample tbBizPlaceOrderDiscountItemExample = new TbBizPlaceOrderDiscountItemExample();
        tbBizPlaceOrderDiscountItemExample.createCriteria().andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlaceOrderDiscountItemMapper.updateByExampleSelective(tbBizPlaceOrderDiscountItem,tbBizPlaceOrderDiscountItemExample);

        BigDecimal receivedAmount = new BigDecimal(0);
        for (TbBizPlaceOrderItem OrderItem : tbBizPlaceOrderItemList){
            receivedAmount = receivedAmount.add(OrderItem.getSalePrice());
        }
        //增加pos支付明细
        TbBizPlaceOrder tOrder=tbBizPlaceOrders1.get(0);
	   	 if(!StringUtils.isEmpty(tOrder.getIsType())&&tOrder.getIsType()==1){//如果是pos，订单生成支付明细
	   		TbBizPlacePayItemExample tbBizPlacePayItemExample=new TbBizPlacePayItemExample();
	   		TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria=tbBizPlacePayItemExample.createCriteria();
	   		tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tOrder.getPlaceOrderId());
	   		List<TbBizPlacePayItem> payItems= tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
	   		if(CollectionUtils.isEmpty(payItems)){//防止重复回调生成支付明细
	   			TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
					tbBizPlacePayItem.setPlaceOrderId(tOrder.getPlaceOrderId());
					tbBizPlacePayItem.setAmount(receivedAmount);
					tbBizPlacePayItem.setAmountType(PlaceConstants.DEBIT_TYPE);
					tbBizPlacePayItem.setPayType(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY));
					tbBizPlacePayItem.setAdminPayType(PayTypeEnum.getEnum(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY)).getUnifiedId());
					if (tbBizPlacePayItemMapper.insertSelective(tbBizPlacePayItem) != 0) {
						logger.debug("========================插入订单支付数据:TbBizPlacePayItem:{}",
								JSON.toJSONString(tbBizPlacePayItem));
		             }  
	   		  }
	   		
	   	 }
        //
        //回调允许更新库存
        if (reqOrderOperate.isUpdateStock()){
            //场地订单明细
            List<TbBizPlaceOrderItem> placeUnitOrderItemList = new ArrayList<>();;
            //门票订单明细
            List<TbBizPlaceOrderItem> placeTicketOrderItemList = new ArrayList<>();
            //小商品订单明细
            List<TbBizPlaceOrderItem> placeGoodsOrderItemList = new ArrayList<>();
            for (TbBizPlaceOrderItem tbOrderItem : tbBizPlaceOrderItemList){
                //场地
                if (tbOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
                        || tbOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)
                        || tbOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_APP_BILLING)){
                    placeUnitOrderItemList.add(tbOrderItem);
                }
                //门票
                else if (tbOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_TICKET)){
                    placeTicketOrderItemList.add(tbOrderItem);
                }
                //小商品
                else if (tbOrderItem.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)){
                    placeGoodsOrderItemList.add(tbOrderItem);
                }
            }
            if (!org.springframework.util.CollectionUtils.isEmpty(placeUnitOrderItemList)){
                //减场地库存
                try {
                    iBaseService.placeBillingUpdatePlaceUnitSalePlan(placeUnitOrderItemList,PlaceConstants.PLACE_ORDER_STATUS);
                } catch (Exception e) {
                    e.printStackTrace();
                    return APIResponse.returnFail("减场地库存异常");
                }
            }
            if (!org.springframework.util.CollectionUtils.isEmpty(placeTicketOrderItemList)){
                //修改门票统计
                try {
                    placeTicketService.ticketStatisticsForScanPay(tbBizPlaceOrders1.get(0).getPlaceId(),placeTicketOrderItemList);
                } catch (Exception e) {
                    e.printStackTrace();
                    return APIResponse.returnFail("修改门票统计异常");
                }
            }
            if (!org.springframework.util.CollectionUtils.isEmpty(placeGoodsOrderItemList)){
                //减商品库存
                try {
                    placeGoodsService.updateGoodsInventory(placeGoodsOrderItemList,tbBizPlaceOrders1.get(0));
                } catch (Exception e) {
                    e.printStackTrace();
                    return APIResponse.returnFail("减商品库存异常");
                }
            }
        }

        //明细转流水
        List<ReqOrderHistory> reqOrderHistoryList = placeOrderUtilService.tbBizPlaceOrderItem2ReqOrderHistory(tbBizPlaceOrders1.get(0), tbBizPlaceOrderItemList, (byte) 0, (byte) 0,null);
        if (!com.alibaba.dubbo.common.utils.CollectionUtils.isEmpty(reqOrderHistoryList)) {
            try {
                placeOrderUtilService.shoppingCartOrderInsertOrderHistory(reqOrderHistoryList);
            } catch (Exception e) {
                e.printStackTrace();
                return APIResponse.returnFail(e.getMessage());
            }
        }


        return APIResponse.returnSuccess("订单支付完成！");
    }

    //订单退款中
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> shoppingCartOrderUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        return APIResponse.returnSuccess("订单设置退款中！");
    }

    //订单退款完成
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> shoppingCartOrderCompleteUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        TbBizPlaceOrder record = new TbBizPlaceOrder();
        //设置已退款状态
        if (reqOrderOperate.getExceptionFlag()){
            record.setPayStatus(PlaceConstants.REFUNDING_EXCEPTION_STATUS);
        }
        else {
            record.setPayStatus(PlaceConstants.REFUNDING_STATUS);
        }

        BigDecimal debit = new BigDecimal(0);
        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria = tbBizPlacePayItemExample.createCriteria();
        tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        tbBizPlacePayItemExample.createCriteria().andPayTypeNotEqualTo(Integer.parseInt(PlaceConstants.PLACE_ADMIN_PAY_WAYS_GIVE));//排除赠送
        List<TbBizPlacePayItem> tbBizPlacePayItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);

        for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemList){
            debit = debit.add(tbBizPlacePayItem.getAmount());
        }

        //设置订单状态和支付状态为已经退款状态
        List<Byte> orderStatus = new ArrayList<>();
        orderStatus.add(PlaceConstants.ORDER_PAYED_STATUS);
        orderStatus.add(PlaceConstants.ORDER_REFUNDING_STATUS);

        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        tbBizPlaceOrderCriteria.andOrderStatusIn(orderStatus);
        List<TbBizPlaceOrder> tbBizPlaceOrders1 = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);

        if (tbBizPlaceOrders1.get(0).getOrderStatus().intValue() > 4){
            return APIResponse.returnFail("订单不能重复退款！");
        }

        record.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setRefundTime(date);
        record.setCredit(debit);

        //退订单状态
        int flag = tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample);
        if (flag != 1){
            return APIResponse.returnFail("退款订单失败");
        }

        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemCriteria.andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);

        //退订单明细
        TbBizPlaceOrderItem tbBizPlaceOrderItem = new TbBizPlaceOrderItem();
        tbBizPlaceOrderItem.setStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
        tbBizPlaceOrderItem.setSurplusGoodsCount(0);
        tbBizPlaceOrderItemMapper.updateByExampleSelective(tbBizPlaceOrderItem,tbBizPlaceOrderItemExample);

        //更改订单支付明细的状态为退款状态
        TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
        tbBizPlacePayItem.setAmountType(PlaceConstants.CREDIT_TYPE);
        tbBizPlacePayItemExample.createCriteria().andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlacePayItemMapper.updateByExampleSelective(tbBizPlacePayItem, tbBizPlacePayItemExample);
        //退优惠明细
        TbBizPlaceOrderDiscountItem tbBizPlaceOrderDiscountItem = new TbBizPlaceOrderDiscountItem();
        tbBizPlaceOrderDiscountItem.setStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
        TbBizPlaceOrderDiscountItemExample tbBizPlaceOrderDiscountItemExample = new TbBizPlaceOrderDiscountItemExample();
        tbBizPlaceOrderDiscountItemExample.createCriteria().andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlaceOrderDiscountItemMapper.updateByExampleSelective(tbBizPlaceOrderDiscountItem,tbBizPlaceOrderDiscountItemExample);

         //5. 修改退款单状态
		  PlaceRefundInfo placeRefundInfo=new PlaceRefundInfo();
		  placeRefundInfo.setRefundNo(reqOrderOperate.getOutTradeRefundOrderNo());
		  placeRefundInfo.setRefundStatus(PlaceConstants.ORDER_REFUND_STATUS.intValue());
		  placeRefundInfo.setRefundTime(new Date());
		  placeRefundInfo.setOrderNo(reqOrderOperate.getOrderNo());		
		  placeRefundInfo.setRefundOrderNo(reqOrderOperate.getRefundOrderNo());
		  refundOrderMapper.updateRefundOrderStatus(placeRefundInfo);
		  
        //场地订单明细
        List<TbBizPlaceOrderItem> placeUnitOrderItemList = new ArrayList<>();;
        //门票订单明细
        List<TbBizPlaceOrderItem> placeTicketOrderItemList = new ArrayList<>();
        //小商品订单明细
        List<TbBizPlaceOrderItem> placeGoodsOrderItemList = new ArrayList<>();

        for (TbBizPlaceOrderItem tbBizPlaceOrderItemTmp : tbBizPlaceOrderItemList){
            //场地
            if (tbBizPlaceOrderItemTmp.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
                    || tbBizPlaceOrderItemTmp.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)
                    || tbBizPlaceOrderItemTmp.getUseType().equals(PlaceConstants.ORDER_USE_APP_BILLING)){
                placeUnitOrderItemList.add(tbBizPlaceOrderItemTmp);
            }
            //门票
            else if (tbBizPlaceOrderItemTmp.getUseType().equals(PlaceConstants.ORDER_USE_TICKET)){
                placeTicketOrderItemList.add(tbBizPlaceOrderItemTmp);
            }
            //小商品
            else if (tbBizPlaceOrderItemTmp.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)){
                placeGoodsOrderItemList.add(tbBizPlaceOrderItemTmp);
            }
        }

        try {
            if (!org.springframework.util.CollectionUtils.isEmpty(placeUnitOrderItemList)){
                //释放场地库存
                placeOrderService.refundUpdateUnitSalePlan(placeUnitOrderItemList,PlaceConstants.PLACE_PREORDER_STATUS);
            }
            if (!org.springframework.util.CollectionUtils.isEmpty(placeTicketOrderItemList)){
                //修改门票统计
                placeTicketService.updateTicketStatistics(tbBizPlaceOrders1.get(0), placeTicketOrderItemList);
            }
            if (!org.springframework.util.CollectionUtils.isEmpty(placeGoodsOrderItemList)){
                //增加商品库存
                placeGoodsService.addGoodsInventory(tbBizPlaceOrders1.get(0),placeGoodsOrderItemList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return APIResponse.returnFail("退款释放库存异常");
        }

        if (tbBizPlaceOrder.getOrderStatus()< 5 ){
            List<ReqOrderHistory> reqOrderHistoryList = placeOrderUtilService.tbBizPlaceOrderItem2ReqOrderHistory(tbBizPlaceOrder, tbBizPlaceOrderItemList, (byte) 1, (byte) 0,null);
            if (!com.alibaba.dubbo.common.utils.CollectionUtils.isEmpty(reqOrderHistoryList)) {
                try {
                    placeOrderUtilService.shoppingCartOrderInsertOrderHistory(reqOrderHistoryList);
                } catch (Exception e) {
                    e.printStackTrace();
                    return APIResponse.returnFail(e.getMessage());
                }
            }
        }

        return APIResponse.returnSuccess("订单退款成功！");
    }

    //订单退款异常
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public APIResponse<?> shoppingCartOrderCancleToUnsubscribeOperate(ReqOrderOperate reqOrderOperate, TbBizPlaceOrder tbBizPlaceOrder) {
        //退款默认设置成退款中
        return APIResponse.returnSuccess();
    }


    /**
     * 门票会员卡退款处理
     * @param reqOrderOperate
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    private APIResponse orderCardPayUnsubscribeOperate(ReqOrderOperate reqOrderOperate) {
        //根据订单号查询使用的会员卡
        TbBizPlaceMemberOpHistoryExample  tbBizPlaceMemberOpHistoryExample = new TbBizPlaceMemberOpHistoryExample();
        TbBizPlaceMemberOpHistoryExample.Criteria tbBizPlaceMemberOpHistoryCriteria = tbBizPlaceMemberOpHistoryExample.createCriteria();
        tbBizPlaceMemberOpHistoryCriteria.andOrderNoEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceMemberOpHistory> tbBizPlaceMemberOpHistoryList = tbBizPlaceMemberOpHistoryMapper.selectByExample(tbBizPlaceMemberOpHistoryExample);

        if(CollectionUtils.isEmpty(tbBizPlaceMemberOpHistoryList)){
            return APIResponse.returnFail("不存在该订单信息！");
        }
        TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistoryTemp = tbBizPlaceMemberOpHistoryList.get(0);
        TbBizPlaceMemberCard tbBizPlaceMemberCard = placeMemberService.getPlaceMemberCardInfo(tbBizPlaceMemberOpHistoryList.get(0).getInnerCardNo(),reqOrderOperate.getPlaceId(),reqOrderOperate.getUid());

        //设置订单状态和支付状态为已经退款状态
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceOrder> tbBizPlaceOrders = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);

        TbBizPlaceOrder record = new TbBizPlaceOrder();
        record.setPayStatus(PlaceConstants.REFUNDING_STATUS);
        record.setOrderStatus(PlaceConstants.ORDER_REFUND_STATUS);
        Date date = new Date();
        record.setOrderStatusChangeDate(date);
        record.setRefundTime(date);
        record.setEndTime(date);
        record.setCredit(tbBizPlaceOrders.get(0).getDebit());
        tbBizPlaceOrderMapper.updateByExampleSelective(record, tbBizPlaceOrderExample);

        //更改订单支付明细的状态为退款状态
        TbBizPlacePayItem tbBizPlacePayItem = new TbBizPlacePayItem();
        tbBizPlacePayItem.setAmountType(PlaceConstants.CREDIT_TYPE);
        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        TbBizPlacePayItemExample.Criteria tbBizPlacePayItemCriteria = tbBizPlacePayItemExample.createCriteria();
        tbBizPlacePayItemCriteria.andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        tbBizPlacePayItemMapper.updateByExampleSelective(tbBizPlacePayItem, tbBizPlacePayItemExample);

        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(reqOrderOperate.getOrderNo());
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItems = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);

        //释放库存
        try {
            placeTicketService.updateTicketStatistics(tbBizPlaceOrders.get(0),tbBizPlaceOrderItems);
        } catch (Exception e) {
            e.printStackTrace();
        }

        int refundNum = 0;

        //增加订单金额
        TbBizPlaceMemberCardExample tbBizPlaceMemberCardExample = new TbBizPlaceMemberCardExample();
        TbBizPlaceMemberCardExample.Criteria tbBizPlaceMemberCardCriteria = tbBizPlaceMemberCardExample.createCriteria();
        tbBizPlaceMemberCardCriteria.andInnerCardNoEqualTo(tbBizPlaceMemberOpHistoryList.get(0).getInnerCardNo());
        TbBizPlaceMemberCard tbBizPlaceMemberCardAfter = new TbBizPlaceMemberCard();

        TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory = new TbBizPlaceMemberOpHistory();

        //如果为储值赠送卡，加上本金金额以及赠送金额
        if(PlaceConstants.PLACE_MEMBER_CARD_TYPE_VALUE.equals(tbBizPlaceMemberCard.getCardType())){
            BigDecimal surplusAmount = tbBizPlaceMemberOpHistoryTemp.getOpBeforeAmount().subtract(tbBizPlaceMemberOpHistoryTemp.getOpAfterAmount());
            BigDecimal surplusGiveAmount = tbBizPlaceMemberOpHistoryTemp.getOpBeforeGiveAmount().subtract(tbBizPlaceMemberOpHistoryTemp.getOpAfterGiveAmount());
            tbBizPlaceMemberCardAfter.setSurplusAmount(surplusAmount.add(tbBizPlaceMemberCard.getSurplusAmount()).setScale(2,BigDecimal.ROUND_HALF_UP));
            tbBizPlaceMemberCardAfter.setSurplusGiveAmount(surplusGiveAmount.add(tbBizPlaceMemberCard.getSurplusGiveAmount()).setScale(2,BigDecimal.ROUND_HALF_UP));

        }
        //如果为储值折扣卡直接加上本金金额
        else if(PlaceConstants.PLACE_MEMBER_CARD_TYPE_DISCOUNT.equals(tbBizPlaceMemberCard.getCardType())){
            BigDecimal surplusAmount = tbBizPlaceMemberOpHistoryTemp.getOpBeforeAmount().subtract(tbBizPlaceMemberOpHistoryTemp.getOpAfterAmount());
            tbBizPlaceMemberCardAfter.setSurplusGiveAmount(tbBizPlaceMemberCard.getSurplusGiveAmount());
            tbBizPlaceMemberCardAfter.setSurplusAmount(surplusAmount.add(tbBizPlaceMemberCard.getSurplusAmount()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        //储值赠送折扣卡处理，加上本金金额以及赠送金额
        else if(PlaceConstants.PLACE_MEMBER_CARD_TYPE_VALUE_DISCOUNT.equals(tbBizPlaceMemberCard.getCardType())){
            BigDecimal surplusAmount = tbBizPlaceMemberOpHistoryTemp.getOpBeforeAmount().subtract(tbBizPlaceMemberOpHistoryTemp.getOpAfterAmount());
            BigDecimal surplusGiveAmount = tbBizPlaceMemberOpHistoryTemp.getOpBeforeGiveAmount().subtract(tbBizPlaceMemberOpHistoryTemp.getOpAfterGiveAmount());
            tbBizPlaceMemberCardAfter.setSurplusAmount(surplusAmount.add(tbBizPlaceMemberCard.getSurplusAmount()).setScale(2,BigDecimal.ROUND_HALF_UP));
            tbBizPlaceMemberCardAfter.setSurplusGiveAmount(surplusGiveAmount.add(tbBizPlaceMemberCard.getSurplusGiveAmount()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        //次卡退款
        else if(PlaceConstants.PLACE_MEMBER_CARD_TYPE_NUMBER.equals(tbBizPlaceMemberCard.getCardType())){
            refundNum = tbBizPlaceMemberOpHistoryTemp.getOpCardCount();
            tbBizPlaceMemberCardAfter.setSurplusNumbers(tbBizPlaceMemberCard.getSurplusNumbers()+tbBizPlaceMemberOpHistoryTemp.getOpCardCount());
        }

        placeMemberService.updatePlaceMemberCardInfo(tbBizPlaceMemberCardExample,tbBizPlaceMemberCardAfter);

        //获取订单号
        String orderNo = CommonUtils.generateBusinessOrderNo(Constants.PLACE_SYSTEM, reqOrderOperate.getUid());
        //获取流水号
        String no = CommonUtils.generateNo(reqOrderOperate.getPlaceId());
        //插入流水记录表
        tbBizPlaceMemberOpHistory.setNo(no);//传入流水号
        tbBizPlaceMemberOpHistory.setOrderNo(orderNo);//存入本次的订单号
        tbBizPlaceMemberOpHistory.setRefundNo(tbBizPlaceMemberOpHistoryList.get(0).getOrderNo());//存入需要退款的订单号
        tbBizPlaceMemberOpHistory.setUid(reqOrderOperate.getUid());
        tbBizPlaceMemberOpHistory.setUserName(tbBizPlaceMemberOpHistoryList.get(0).getUserName());
        tbBizPlaceMemberOpHistory.setMobile(tbBizPlaceMemberOpHistoryList.get(0).getMobile());
        tbBizPlaceMemberOpHistory.setInnerCardNo(tbBizPlaceMemberOpHistoryList.get(0).getInnerCardNo());
        tbBizPlaceMemberOpHistory.setCardTypeName(tbBizPlaceMemberCard.getCardName());
        tbBizPlaceMemberOpHistory.setAmount(tbBizPlaceMemberOpHistoryTemp.getAmount());
        String opTypeName = tbBizPlaceMemberOpHistoryList.get(0).getOpTypeName();
        tbBizPlaceMemberOpHistory.setOpTypeName(opTypeName + "退款");
        tbBizPlaceMemberOpHistory.setOpBeforeAmount(tbBizPlaceMemberCard.getSurplusAmount());
        tbBizPlaceMemberOpHistory.setOpBeforeGiveAmount(tbBizPlaceMemberCard.getSurplusGiveAmount());
        tbBizPlaceMemberOpHistory.setOpAfterAmount(tbBizPlaceMemberCardAfter.getSurplusAmount());
        tbBizPlaceMemberOpHistory.setOpAfterGiveAmount(tbBizPlaceMemberCardAfter.getSurplusGiveAmount());
        tbBizPlaceMemberOpHistory.setPlaceId(reqOrderOperate.getPlaceId());
        tbBizPlaceMemberOpHistory.setSrc(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_REFUND);
        tbBizPlaceMemberOpHistory.setSourceType(tbBizPlaceMemberOpHistoryList.get(0).getSourceType());
        tbBizPlaceMemberOpHistory.setState(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_YES);
        tbBizPlaceMemberOpHistory.setOpBeforeNumbers(tbBizPlaceMemberCard.getSurplusNumbers());
        tbBizPlaceMemberOpHistory.setOpAfterNumbers(tbBizPlaceMemberCardAfter.getSurplusNumbers());
        tbBizPlaceMemberOpHistory.setOpCardCount(tbBizPlaceMemberOpHistoryTemp.getOpCardCount());
        tbBizPlaceMemberOpHistory.setMemo("会员卡退款");
        tbBizPlaceMemberOpHistory.setType(PlaceConstants.PLACE_MEMBER_HISTORY_TYPE_RECHARGE);
        tbBizPlaceMemberOpHistory.setCreateUid(reqOrderOperate.getUid());
        tbBizPlaceMemberOpHistory.setPayType(PlaceConstants.PAY_WAY_FOR_CARD);
        tbBizPlaceMemberOpHistory.setPlatform(reqOrderOperate.getPlatformType());
        tbBizPlaceMemberOpHistoryMapper.insertSelective(tbBizPlaceMemberOpHistory);

        /*//场馆报名人数减一
        Map<String,Object> map = new HashMap<>();
        map.put("placeId", tbBizPlaceOrders.get(0).getPlaceId());
        map.put("subtractSignupCount", 1);
        placeMapper.updateCount(map);*/

        /*try {
            this.searchService.addPlaceIndex(tbBizPlace, null);
        } catch (Exception e) {
            logger.error("增加索引失败，加入消息队列进行处理!");
            //加入消息队列进行异步处理
            TbBizPlaceVo tbBizPlaceVo = new TbBizPlaceVo();
            tbBizPlaceVo.setTbBizPlace(tbBizPlace);
            mqPlaceProductWrapper.addPlaceIndexAsyncSend(tbBizPlaceVo);
        }*/

        if (tbBizPlaceOrders.get(0).getOrderStatus()< 5 ){
            if (refundNum > 0) {
                //插入退款流水
                ReqOrderHistory reqOrderHistory = placeOrderUtilService.memberTicket2ReqOrderHistory(tbBizPlaceOrders.get(0), refundNum, (byte) 1, (byte) 0);
                try {
                    placeOrderUtilService.insertOrderHistory(reqOrderHistory);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                apiPlaceTicketService.orderCallBackHistory(reqOrderOperate);
            }
        }


        return APIResponse.returnSuccess("订单退款成功！");

    }

    private int getNumMemberCardOrderOpNum(String placeOrderId){
        TbBizPlaceMemberOpHistoryExample tbBizPlaceMemberOpHistoryExample = new TbBizPlaceMemberOpHistoryExample();
        tbBizPlaceMemberOpHistoryExample.createCriteria().andOrderNoEqualTo(placeOrderId);
        List<TbBizPlaceMemberOpHistory> tbBizPlaceMemberOpHistoryList = tbBizPlaceMemberOpHistoryMapper.selectByExample(tbBizPlaceMemberOpHistoryExample);
        if (CollectionUtils.isEmpty(tbBizPlaceMemberOpHistoryList)){
            return 0;
        }
        TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory = tbBizPlaceMemberOpHistoryList.get(0);
        if (StringUtils.isEmpty(tbBizPlaceMemberOpHistory.getOpCardCount())){
            return 0;
        }
        if (tbBizPlaceMemberOpHistory.getOpCardCount().intValue() == 0){
            return 0;
        }
        return tbBizPlaceMemberOpHistory.getOpCardCount();
    }
}
